package com.kukudb.asaproject.controller.api;


import com.kukudb.asaproject.pojo.LicenseUsageLogs;
import com.kukudb.asaproject.pojo.ResponseMessage;
import com.kukudb.asaproject.pojo.ToEmail;
import com.kukudb.asaproject.pojo.User;
import com.kukudb.asaproject.pojo.paramsDto.CodeEmailDto;
import com.kukudb.asaproject.pojo.paramsDto.LoginParamDto;
import com.kukudb.asaproject.pojo.paramsDto.RegisterParamDto;
import com.kukudb.asaproject.pojo.resultDto.UserInfoDto;
import com.kukudb.asaproject.serveice.CodeServericeInterfaceImpl;
import com.kukudb.asaproject.serveice.LicenseUsageLogsImpl;
import com.kukudb.asaproject.serveice.interfaceTemp.UserServiceInterface;
import jakarta.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController     //接口方法返回对象  转换成json数据
@RequestMapping("/api/user")    // localhost:81/api/user
public class UserApi {
    @Autowired
    UserServiceInterface userService;
    @Autowired
    CodeServericeInterfaceImpl codeServerice;

    @Autowired
    LicenseUsageLogsImpl licenseUsageLogs;
    /**
     * 日志配置
     */
    Logger logger = LoggerFactory.getLogger(UserApi.class);
    @PostMapping("/register")
    public ResponseMessage<String> register(@Validated @RequestBody RegisterParamDto registerParam, HttpSession session) {
        logger.info("注册用户："+registerParam.getUserName());
        if(!registerParam.getCode().equals(session.getAttribute("code"))){
            return ResponseMessage.error("验证码错误");
        }
        User user = new User();
        BeanUtils.copyProperties(registerParam,user);
        userService.addUser(user);
        //赠送积分

        return ResponseMessage.success("注册成功");
    }

    /**
     * 登录用户
     *
     * @param LoginParam
     * @param session
     * @return
     */
    @PostMapping("/login")
    public ResponseMessage<UserInfoDto> login(@RequestBody LoginParamDto LoginParam, HttpSession session) {
        // Implement user registration logic here
        logger.info("登录用户："+ LoginParam);
        User user = new User();
        BeanUtils.copyProperties(LoginParam,user);
        User login = userService.login(user, session);
        UserInfoDto userInfo = new UserInfoDto();
        BeanUtils.copyProperties(login,userInfo);
        logger.info("登录用户："+ userInfo.getUserName());
        return ResponseMessage.success(userInfo);
    }

    @GetMapping("/logout")
    public ResponseMessage<String> logout(HttpSession session) {
        // Implement user registration logic here
        logger.info("登出用户："+ session.getAttribute("token"));
        session.invalidate();
        return ResponseMessage.success("登出成功");
    }

    /**
     * 获取验证码
     * @param session
     * @return
     */
    @PostMapping("/getCode")
    public ResponseMessage<String> getCode(@RequestBody CodeEmailDto emailAddress, HttpSession session) {
        logger.info("获取验证码："+ emailAddress);
        ToEmail email = new ToEmail(new String[]{emailAddress.getEmailAddress()},"验证码","验证码");
        codeServerice.sendCode(email,session);
        logger.info("发送验证码成功，getCode()执行完毕");
        return ResponseMessage.success("获取验证码成功");
    }
}
